package com.gper.edu.flink.stream.window;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: ellis.guan
 * @Description:
 * @Date: 2020/11/8 13:15
 * @Version: 1.0
 */
public class MyWindowSum implements WindowFunction<Tuple2<Integer,Double>, Tuple3<Integer,Double,String>,Integer, TimeWindow> {

    @Override
    public void apply(Integer key, TimeWindow window, Iterable<Tuple2<Integer, Double>> input, Collector<Tuple3<Integer, Double, String>> out) throws Exception {
        Tuple2<Integer, Double> value = input.iterator().next();
        out.collect(Tuple3.of(value.f0,value.f1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(window.getEnd()))));
    }
}
